﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using webapp4U.BOL;
using webapp4U;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>

namespace webapp4U.DAL
{
    public class ShoppingCartDAL
    {
       

        #region ObjIDataReader

        /// <summary>
        /// Returns a new Products instance filled with the DataReader's current record data 
        /// </summary>
        private ShoppingCart GetShoppingCartFromReader(IDataReader dr)
        {
            ShoppingCart obj = new ShoppingCart();
            //obj.ProductID = (dr["ProductID"] is DBNull) ? Int32.MinValue : (Int32)dr["ProductID"];
            //obj.AddedDate = (dr["AddedDate"] is DBNull) ? DateTime.MinValue : (DateTime)dr["AddedDate"];
            //obj.AddedBy = (dr["AddedBy"] is DBNull) ? String.Empty : (String)dr["AddedBy"];
            obj.ItemKey = (dr["itemkey"] is DBNull) ? String.Empty : (String)dr["itemkey"];
            obj.Description = (dr["description"] is DBNull) ? String.Empty : (String)dr["description"];
            obj.UnitPrice = (dr["price"] is DBNull) ? float.MinValue : float.Parse(dr["price"].ToString());
            obj.Quantity = (dr["shipqty"] is DBNull) ? Int32.MinValue : Int32.Parse(dr["shipqty"].ToString());
            obj.Subtotal = obj.UnitPrice * obj.Quantity;
            obj.ImageUrl = (dr["ImgName"] is DBNull) ? String.Empty : (String)dr["ImgName"];

            return obj;
        }
        #endregion

        private List<ShoppingCart> GetShoppingCartCollectionFromReader(IDataReader dr)
        {

            List<ShoppingCart> obj = new List<ShoppingCart>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    obj.Add(GetShoppingCartFromReader(dr));
                }
                dr.Close();
            }
            return obj;
        }

        // Procced Order
        public DataTable dtListProvince(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.Get_Data_DataTable(Configuration.CurrentConnectionString, ref error, "TTr_GetProvince", CommandType.StoredProcedure);
        }

        public DataTable dtListTownByProvinceCode(ref string error, string ProvinceCode)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.Get_Data_DataTable(Configuration.CurrentConnectionString, ref error, "TTr_GetTownByProvinceCode", CommandType.StoredProcedure,
                 new SqlParameter("@ProvinceCode", ProvinceCode));
        }
        public DataTable FindCustomerByPhoneOrIdNo(ref string error, string PhoneOrIdNo)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.Get_Data_DataTable(Configuration.CurrentConnectionString, ref error, "TTr_FindCustomerByPhoneOrIdNo", CommandType.StoredProcedure,
                 new SqlParameter("@PhoneOrIDCardNo", PhoneOrIdNo));
        }
        public DataTable GetOrderTempByCustomerKey(ref string error, string CustommerKey)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.Get_Data_DataTable(Configuration.CurrentConnectionString, ref error, "TTr_GetOrderTempByCustomerKey", CommandType.StoredProcedure,
                 new SqlParameter("@customer_key", CustommerKey));
        }

        public List<ShoppingCart> GetDetailOrderTemp(String OrderId, ref string error)
        {

            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_GetOrderDetailTempByOrderId",
                CommandType.StoredProcedure, new SqlParameter("@tranno", OrderId));
            return GetShoppingCartCollectionFromReader(dr);

        }
        public List<ShoppingCart> GetItemStartKit(ref string error)
        {

            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "ls_getStartKit",
                CommandType.StoredProcedure);
            return GetShoppingCartCollectionFromReader(dr);

        }


        public string GetCustomerKeyNew(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_getCustomerIDNew", CommandType.StoredProcedure);
        }
        public string GetAndHoleOrderCode(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_getAndHoleOrderCode", CommandType.StoredProcedure);
        }
        public string GetSalesp_key_BASupport(ref string error, string IDNo, string Phone, string ProvinceID, string TownID)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "ls_getBASupport", CommandType.StoredProcedure,
                new SqlParameter("@IDNo", IDNo), new SqlParameter("@Phone", Phone), new SqlParameter("@ProvinceID", ProvinceID), new SqlParameter("@TownID", TownID));
        }
        public string GetSiteCodeBySalesp_key(ref string error, string SalespKey)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_getSiteCodeBySalesp_Key", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", SalespKey));
        }


        public string GetPhoneNumberBA_BySalesp_key(ref string error, string SalespKey)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_GetPhoneNumberOfBA_BySalespKey", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", SalespKey));
        }

        public bool SendSMS(ref string error, string Phone, string OrderCode)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "ls_sendMessage", ref error,
                new SqlParameter("@TelNo", Phone), new SqlParameter("@SMS", OrderCode));
        }

        public bool InsertOrder(ref string error, string Sitecode, string OrderCode, string salesp_key, string UserName, float TongHoaDon,
            string NgayHD, DateTime DateTimeNow, int so, int StattusOrder, string PaymentMethodsID, string ModeOfDeliveryID, string Customer_key)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "TTr_InsertHoaDonBanHangOnline", ref error,
                new SqlParameter("@Sitecode", Sitecode), new SqlParameter("@tranno", OrderCode), new SqlParameter("@salesp_key", salesp_key),
                new SqlParameter("@Recuser", UserName), new SqlParameter("@doctot", TongHoaDon), new SqlParameter("@invdate", NgayHD),
                new SqlParameter("@Recdate", DateTimeNow), new SqlParameter("@so", so), new SqlParameter("@StattusOrder", StattusOrder),
                new SqlParameter("@PaymentMethodsID", PaymentMethodsID), new SqlParameter("@ModeOfDeliveryID", ModeOfDeliveryID),
                new SqlParameter("@customer_key", Customer_key));
        }


        public bool InsertOrderDetail(ref string error, string Sitecode, string MaHD, DateTime DateTimeNow, string UserName, string NgayHD, string MaHang, string TenHang, string LoaiDong, float SoLuong, float Gia, int SoTT)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "TTr_InsertCTHoaDonBanHangOnline", ref error,
                new SqlParameter("@Sitecode", Sitecode),
                new SqlParameter("@tranno", MaHD),
                new SqlParameter("@Recdate", DateTimeNow),
                new SqlParameter("@Recuser", UserName),
                new SqlParameter("@invdate", NgayHD),
                new SqlParameter("@itemkey", MaHang),
                new SqlParameter("@description", TenHang),
                new SqlParameter("@lineitemtyp", LoaiDong),
                new SqlParameter("@shipqty", SoLuong),
                new SqlParameter("@unitprice", Gia),
                new SqlParameter("@SoTT", SoTT)
                );
        }

        public bool InsertCustomerNotRegistry(ref string error, string CustomerKey, string CustomerName, string Address, string Phone, 
            string ProvinceCode, string TownCode, string Recdate, string IDCardNo, string SalespKey, string SiteCode)
        { 
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "TTr_InsertCustomerNotRegistry", ref error,
                new SqlParameter("@customer_key", CustomerKey),
                new SqlParameter("@customer_name", CustomerName),
                new SqlParameter("@address", Address),
                new SqlParameter("@phone", Phone),
                new SqlParameter("@ProvinceCode", ProvinceCode),
                new SqlParameter("@TownCode", TownCode),
                new SqlParameter("@Recdate", Recdate),
                new SqlParameter("@IDCardNo", IDCardNo),
                new SqlParameter("@SalespKey", SalespKey),
                new SqlParameter("@SiteCode", SiteCode));
        }

        public bool DeleteOrderAndOrderDetailOnlineTamGiu(ref string error, string MaHD)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "TTr_DeleteOrderAndOrderDetailOnlineTamGiu", ref error,
                new SqlParameter("@tranno", MaHD));
        }

        public bool DeleteCustomer(ref string error, string CustomerKey)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return obj.ExecuteSQl_bool(Configuration.CurrentConnectionString, CommandType.StoredProcedure, "TTr_DeleteCustomer", ref error,
                new SqlParameter("@customer_key", CustomerKey));
        }
        
        //kiểm tra dơn hàng đau tiên
        public bool CheckFirstOrderBySalespKey(ref string error, string SalespKey)
        {
            string strResult = "";
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            strResult= obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_CheckFirstOrderBySalespKey", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", SalespKey));
            if (strResult == "True")
                return true;
            else
                return false;
                    
        }

        // Get ProductsCountByInClassKey
        //public int GetProductsCountByInClassKey(string InClassKey, ref string error)
        //{
        //    ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
        //    return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountProductByInClassKey", CommandType.StoredProcedure, new SqlParameter("@InClassKey", InClassKey)));
        //}

       
    }
}